package com.yatuhashi.manual.mhp3.parse;

import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Xml;

import com.yatuhashi.manual.mhp3.bean.MonsterBean;
import com.yatuhashi.manual.mhp3.db.DataBaseHelper;
import com.yatuhashi.manual.mhp3.util.LogUtil;

public class MonsterParser {
    private Context mContext;
    
    public MonsterParser(Context context) {
        mContext = context;
    }
    
    public boolean parse(InputStream is) {
        boolean success = false;
        
        MonsterBean monster = null;
        int counter = 0;
        
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(is, "UTF-8");
            
            int eventType = parser.getEventType();
            
            while(eventType!=XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    String startTag = parser.getName();
                    if("root".equals(startTag)) {
                        
                    } else if("item".equals(startTag)) {
                        monster = new MonsterBean(); 
                        monster.setId(counter);
                    } else if("name".equals(startTag)) {
                        monster.setName(parser.nextText());
                    } else if("alias".equals(startTag)) {
                        monster.setAlias(parser.nextText());
                    } else if("race".equals(startTag)) {
                        monster.setRace(parser.nextText());
                    } else if("rid".equals(startTag)) {
                        monster.setRid(Integer.parseInt(parser.nextText()));
                    } else if("weakness".equals(startTag)) {
                        monster.setWeakness(parser.nextText());
                    } else if("mid".equals(startTag)) {
                        monster.setMid(Integer.parseInt(parser.nextText()));
                    }
                    break;
                case XmlPullParser.END_TAG:
                    String endTag = parser.getName();
                    if("item".equals(endTag)) {
                        registDb(monster);
                        counter++;
                    }
                    break;
                }
                eventType = parser.next();
            }
            success = true;
        } catch (Exception e) {
            LogUtil.logE(e.toString());
            success = false;
        }
        return success;
    }
    
    private boolean registDb(MonsterBean monster) {
        DataBaseHelper dbHelper = new DataBaseHelper(mContext);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        String sql = 
            "insert into monster_table (" +
            "name, " +
            "alias, " +
            "race, " +
            "rid, " +
            "weakness, " +
            "mid) values('"
            + monster.getName()
            + "','"
            + monster.getAlias()
            + "','"
            + monster.getRace()
            + "','"
            + monster.getRid()
            + "','"
            + monster.getWeakness()
            + "','"
            + monster.getMid()
            + "');";
        
        db.execSQL(sql);
        
        return true;
    }
}
